package A_review.review9_22;


import java.util.Deque;
import java.util.LinkedList;

//TODO:2023/9/22 单调栈
public class Class739 {

    public  int[]  find(int [] nums){
        if (nums.length==1){
            return  new int[]{0};
        }
        int[] result = new int[nums.length];
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(0);

        for (int i = 1; i < nums.length; i++) {
            if (nums[i]<=nums[(int)stack.peek()]){
                stack.push(i);
            }else {
                while (!stack.isEmpty()&&nums[i]>nums[(int)stack.peek()]){
                    result[(int)stack.peek()]=i-(int)stack.peek();
                    stack.pop();
                }
                //TODO:2023/9/22  忘了加上这一句
                stack.push(i);
            }
        }
        return result;
    }

    public  int[]  dailyTempratures(int[]  temp) {
        int length = temp.length;
        int[] res = new int[length];
        Deque stack = new LinkedList<>();
        stack.push(0);
        for (int i = 1; i < length; i++) {

            if (temp[i] <= temp[(int) stack.peek()]) {
                stack.push(i);
            }
            else {
                while (!stack.isEmpty() && temp[i] > temp[(int) stack.peek()]) {
                    res[(int) stack.peek()] = i - (int) stack.peek();
                    stack.pop();
                }
                stack.push(i);
            }

        }
        return res;
    }
}
